<template>
  <div class="farmland-page f-c">
    <div class="f-r j-b">
      <el-form inline class="f-g">
        <el-form-item label="选择田分组">
          <el-select v-model="searchParams.groupId" clearable placeholder="请选择" @change="changeGroup">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.groupName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <el-button style="margin-bottom: 18px" type="primary" icon="el-icon-plus" @click="onAdd">新增田</el-button>
    </div>
    <div class="content f-g">
      <div class="header f-r a-c j-e">
        <SwitchTabs
          v-model="componentName"
          style="margin-right: 0"
          :data="[{ name: '列表模式', value: 'FarmlandList'},{ name: '田分组', value: 'FarmlandGroup'},{ name: '地图模式', value: 'FarmlandMap'}, ]"
          @change="(val) => $emit('change', val)"
        />
      </div>
      <div class="circle circle1 f-c j-c a-c">
        <div>河北省</div>
        <div>100个</div>
        <div>280亩</div>
      </div>
      <div class="circle circle2 f-c j-c a-c">
        <div>甘肃省</div>
        <div>100个</div>
        <div>280亩</div>
      </div>
      <div class="showList">
        <div :class="['show-btn',showType === 'city' ? 'active' : '']" @click="showType = 'city'">按行政区域显示</div>
        <div :class="['show-btn',showType === 'group' ? 'active' : '']" @click="showType = 'group'">按田分组显示</div>
        <div v-if="group" class="show-btn2">概述</div>
        <div v-if="group" class="show-btn-box f-c j-b">
          <div>
            当前组：{{ group.groupName }}
          </div>
          <div>
            总地块数：{{ group.fieldCount }}
          </div>
          <div>
            总面积：{{ group.areaSum }}亩
          </div>
        </div>
      </div>
      <Map2 ref="map" style="height: calc(100% - 60px)" @load="loadMap" />
    </div>
  </div>
</template>

<script>
import crud from '@/mixins/crud';
import Map2 from './map'
import { http_farmland, http_farmland_group } from '@/api';
export default {
  name: 'FarmlandGroup',
  components: {
    Map2
  },
  mixins: [crud],
  data() {
    return {
      searchParams: {
        groupId: ''
      },
      showType: 'city',
      tableData: [
      ],
      rowItem: {},
      options: [],
      componentName: 'FarmlandMap'
    }
  },
  computed: {
    group() {
      return this.options.find(el => el.id === this.searchParams.groupId)
    }
  },
  mounted() {
    http_farmland_group.page().then(res => {
      this.options = res.rows
    })
  },
  methods: {
    onAdd() {
      this.$router.push({
        path: '/farmland/add'
      })
    },
    changeGroup(id) {
      http_farmland_group.fieldPage({
        id
      }).then(res => {
        console.log(res)
      })
    },
    loadMap() {
      http_farmland.page().then(res => {
        res.rows.forEach((el, index) => {
          if (index === 0) {
            /* 计算边界，绘制地图 */
            const editBoundaries = [];
            const boundaryArr = el.boundary.split(',');
            for (let i = 0; i < boundaryArr.length; i = i + 2) {
              editBoundaries.push({
                lng: boundaryArr[i],
                lat: boundaryArr[i + 1]
              });
            }
            this.$refs.map.map.addBoundy(editBoundaries)
          }
        })
        console.log(res)
      })
    }
  }

}
</script>

<style scoped lang="scss">
.farmland-page {
  position: relative;
  padding: 20px;
  background-color: #ffffff;
  height: 100%;
  .el-form-item {
    margin-bottom: 22px;
  }
  .border {
    border-top: 1px solid #E8E8E8;
    margin-bottom: 27px;
  }
  .row-list {
    .row-box {
      width: 20%;
      padding: 12px 10px;
      .row-item {
        cursor: pointer;
        width: 100%;
        height: 200px;
        box-shadow: 0px 3px 6px 1px rgba(0,0,0,0.16);
        position: relative;
        border-radius: 14px;
        background-color: #58A768;
        .tag {
          position: absolute;
          left: 11px;
          top: -5px;
          width: 36px;
          height: 16px;
          background: #1AAD23;
          line-height: 16px;
          text-align: center;
          color: #ffffff;
          font-size: 12px;
        }
        .title-box {
          flex-grow: 1;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 48px;

          font-weight: 500;
          color: #FFFFFF;
          opacity: 1;
        }
        .name-wrapper {
          padding: 2px 0 0 11px;
          height: 90px;
          width: 100%;
          background-color: #ffffff;
          border-radius:0 0 14px 14px;
          overflow: hidden;
          .avatar {
            position: absolute;
            width: 35px;
            height: 35px;
            box-shadow: 0px 3px 6px 1px rgba(0,0,0,0.16);
            opacity: 1;
            border: 1px solid #707070;
            border-radius: 50%;
            overflow: hidden;
            top: -12px;
            right: 14px;
            background-color: #ffffff;
            z-index: 1999;
          }
          .name {
            font-size: 14px;

            font-weight: 400;
            color: #595959;
            line-height: 22px;
          }
          .desc {
            margin-top: 5px;
            font-size: 12px;

            font-weight: 400;
            color: #595959;
            line-height: 22px;
          }
        }
      }
    }
    .row-item1 {
      background-color: transparent !important;
      img {
        width: 78px;
        height: 62px;
      }
      .title {
        margin-top: 30px;
        font-size: 18px;

        font-weight: 400;
        line-height: 22px;
        color: #9A9A9A;
      }
    }
  }
  .header {
    position: absolute;
    z-index: 1999;
    right: 40px;
    top: 100px;
    .title {
      font-size: 16px;

      font-weight: 400;
      color: #222222;
      line-height: 24px;
    }
    .value {
      font-size: 16px;
      font-family: Source Han Sans CN-Regular, Source Han Sans CN;
      font-weight: 400;
      color: #1AAD23;
      line-height: 14px;
      margin-right: 23px;
      font-weight: bold;
    }
  }
  .showList {
    position: absolute;
    z-index: 1999;
    right: 40px;
    top: 190px;
    .show-btn.active {
      background: rgba(26,173,35,1);
      color: #ffffff;
    }
    .show-btn {
      margin-bottom: 14px;
      width: 174px;
      height: 32px;
      background: #ffffff;
      border-radius: 4px;
      text-align: center;
      line-height: 32px;
      font-size: 14px;
      color: #C3C3C3;
    }
    .show-btn2 {
      width: 174px;
      height: 32px;
      line-height: 32px;
      background: rgba(17,17,17,0.67);
      opacity: 1;
      border-radius: 4px;
      text-align: center;
      color: #ffffff;
      margin: 27px 0 4px 0;
      font-weight: 500;
    }
    .show-btn-box {
      width: 174px;
      height: 87px;
      background: rgba(17,17,17,0.67);
      opacity: 1;
      border-radius: 4px;
      padding: 9px 11px;
      color: #ffffff;
      font-size: 12px;
      font-weight: 500;
    }
  }
  .circle {
    width: 82px;
    height: 82px;
    background: rgba(17,17,17,0.67);
    border-radius: 50%;
    color: #ffffff;
    font-size: 12px;
    line-height: 18px;

    font-weight: 500;
  }
  .circle1 {
    position: absolute;
    z-index: 1999;
    left: 300px;
    top: 200px;
  }
  .circle2{
    position: absolute;
    z-index: 1999;
    left: 600px;
    top: 150px;
  }
}
</style>
